package com.mobartisan.workinglunch.util;

import android.content.Context;
import android.content.SharedPreferences;


/**
 * Created by dell on 2016/3/30.
 */
public class SharedPreferencesUtil {
    private static final String TAG = SharedPreferencesUtil.class.getSimpleName();
    private static SharedPreferencesUtil instance;
    private Context context;

    private String secretKey = "";

    public SharedPreferencesUtil(Context context) {
        this.context = context;
//        secretKey = ResourceUtils.getString(context, R.string.secretkey);
    }

    public static void setInstance(SharedPreferencesUtil instances) {
        instance = instances;
    }

    public static SharedPreferencesUtil getInstance() {
        return instance;
    }


    public String getKey(String loginkey) {
        String key;
        try {
            key = EncodeUtils.encryptMode(loginkey);
        } catch (Exception ex) {
            key = "";
        }
        return key;
    }

    public void setStringValue(String in_settingName, String in_val) {
        String encrypkey = "";
        try {
            encrypkey = EncodeUtils.encryptMode(in_val);
        } catch (Exception ex) {
            encrypkey = "";
        }
        SharedPreferences sp = context.getSharedPreferences(in_settingName, Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = sp.edit();
        ed.putString(in_settingName, in_val);
        ed.commit();
        ed = null;
        sp = null;
    }

    public String getStringValue(String in_settingName) {
        SharedPreferences sp = context.getSharedPreferences(in_settingName, Context.MODE_PRIVATE);
        String ret = sp.getString(in_settingName, "");
        String decrypkey = "";
        if (ObjectUtils.isStringNotEmpty(ret)) {
            //对密码进行AES解密
            try {
                decrypkey = EncodeUtils.decryptMode(ret);
            } catch (Exception ex) {
                decrypkey = "";
            }
        } else
            decrypkey = "";
        sp = null;
        return ret;
    }

    public void clearStringValue(String in_settingName) {
        SharedPreferences sp = context.getSharedPreferences(in_settingName, Context.MODE_PRIVATE);
        sp.edit().clear().commit();
    }

    public void setBooleanValue(String in_settingName, boolean in_val) {
        String ret = "";
        //对密码进行AES解密
        try {
            ret = EncodeUtils.encryptMode(String.valueOf(in_val));
        } catch (Exception ex) {
            ret = "";
        }
        SharedPreferences sp = context.getSharedPreferences(in_settingName, Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = sp.edit();
        ed.putString(in_settingName, ret);
        ed.commit();
        ed = null;
        sp = null;
    }

    public boolean getBooleanValue(String in_settingName) {
        SharedPreferences sp = context.getSharedPreferences(in_settingName, Context.MODE_PRIVATE);
        String ret = sp.getString(in_settingName, "");
        //对密码进行AES解密
        String result = "";
        if (ObjectUtils.isStringNotEmpty(ret)) {
            try {
                result = EncodeUtils.decryptMode(String.valueOf(ret));
            } catch (Exception ex) {
                result = ex.getMessage();
            }
        }
        sp = null;
        if (ObjectUtils.equals("true", result))
            return true;
        else
            return false;
    }

    public void setIntValue(String in_settingName, Integer in_val) {

        SharedPreferences sp = context.getSharedPreferences(in_settingName, Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = sp.edit();
        ed.putInt(in_settingName, in_val);
        ed.commit();
        ed = null;
        sp = null;
    }

    public Integer getIntValue(String in_settingName) {
        SharedPreferences sp = context.getSharedPreferences(in_settingName, Context.MODE_PRIVATE);
        Integer ret = sp.getInt(in_settingName,0);
        String decrypkey = "";
        sp = null;
        return ret;
    }

}
